﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;

namespace CommunergyLogicTests.MockObjects
{
    public class MockRealtimeRepository : IRealtimeRepository
    {
		#region Fields (1) 

        MockUnitOfWork unitOfWork;

		#endregion Fields 

		#region Constructors (1) 

        public MockRealtimeRepository(IUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork as MockUnitOfWork;
        }

		#endregion Constructors 

		#region Methods (2) 

		// Private Methods (2) 

        private List<RealtimeEnergyReading> CleanMock(List<MockRealtime> mockRealtime)
        {
            var clean = (from m in mockRealtime
                         select new RealtimeEnergyReading
                         {
                             Joules = m.Joules,
                             Watts  = m.Watts,
                             ReadingDateTime = m.ReadingDateTime
                         }).ToList();
            return clean;
        }

        private List<MockRealtime> MockRealtime(string meterId, List<RealtimeEnergyReading> realtimeEnergyReadings)
        {
            var mockHistoric = (from r in realtimeEnergyReadings
                                select new MockRealtime
                                {
                                    MeterId = meterId,
                                    ReadingDateTime = r.ReadingDateTime,
                                    Joules = r.Joules,
                                    Watts = r.Watts,
                                }).ToList();
            return mockHistoric;
        }

		#endregion Methods 



        #region IRealtimeRepository Members

        public void Insert_ElectricityRealtime(string meterId, List<RealtimeEnergyReading> realtimeEnergyReadings)
        {
            var mock = MockRealtime(meterId, realtimeEnergyReadings);
            var newPending = unitOfWork.RealtimePending.Union(mock).ToList();
            unitOfWork.RealtimePending = newPending;
            
        }

        public List<RealtimeEnergyReading> Get_ElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            var mock = unitOfWork.RealtimeCommitted.Where(r => r.MeterId == meterId && r.ReadingDateTime >= dateFrom && r.ReadingDateTime <= dateTo).ToList();
            var clean = CleanMock(mock);
            return clean;
        }

    
        public RealtimeEnergyReading GetLastElectricityRealtime(string meterId)
        {
            return unitOfWork.RealtimeCommitted.Last();
        }

      
        public HistoricalEnergyReading GetCumulativeElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            var data = from e in unitOfWork.RealtimeCommitted
                       where e.MeterId == meterId && e.ReadingDateTime >= dateFrom && e.ReadingDateTime < dateTo
                       group e by e.MeterId into rolled
                       select new HistoricalEnergyReading
                       {
                           ReadingDateTime = dateFrom,
                           KWh = ((decimal)rolled.Sum(rt => rt.Joules)) / 3600000,
                           TimeSpan = dateTo.Subtract(dateFrom)
                       };
            return data.First();
        }

        #endregion
    }
}
